package org.railage.server.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.railage.server.HibernateUtil;
import org.railage.server.entities.StationOfRoute;

public class StationOfRouteDAOImpl implements StationOfRouteDAO {

	private static final Logger log = Logger
			.getLogger(StationOfRouteDAOImpl.class);

	@SuppressWarnings("unchecked")
	public List<StationOfRoute> getOrderedStationsOfRoute(String routeName) {
		Session session = null;
		List<StationOfRoute> stations = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Query query = session.getNamedQuery("getOrderedStationsOfRoute");
			query.setString("routeName", routeName);
			stations = (List<StationOfRoute>) query.list();
			for (StationOfRoute station : stations) {
				Hibernate.initialize(station.getRoute());
				Hibernate.initialize(station.getStation());
			}
		} catch (Exception e) {
			log.error("Error occured during Hibernate session:", e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return stations;
	}

}
